package cn.com.bluemoon.daps.sync.vo.syncmanage;

import cn.com.bluemoon.daps.common.enums.SyncStatus;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author Jarod.Kong
 */
@Data
public class SyncManagementDto {
    /**
     * 有效的模型id（目的过滤掉同步任务中模型id对应的模型已被删除的数据）
     */
    private List<String> modelIds;
    /**
     * change_order
     */
    private String changeOrder;
    private Integer needDba;
    private Integer preProdStatus;
    private Integer prodStatus;
    private String tableName;

    public SyncManagementDto() {
    }

    public SyncManagementDto(SyncManagementPageVo searchVo) {
        if (searchVo.getNeedDba() == null) {
            this.needDba = null;
        } else if (searchVo.getNeedDba().equals(Boolean.TRUE)) {
            this.needDba = 1;
        } else {
            this.needDba = 0;
        }
        this.tableName = searchVo.getTableNameTrimAndLowercase();
        this.preProdStatus = initSyncStatus(searchVo.getPreProdStatus());
        this.prodStatus = initSyncStatus(searchVo.getProdStatus());
        this.changeOrder = searchVo.getApplyPublishOrder();
        if (StringUtils.isNotBlank(searchVo.getModelId())) {
            this.modelIds = Collections.singletonList(searchVo.getModelId());
        }
    }

    private Integer initSyncStatus(SyncStatus preProdStatus) {
        if (SyncStatus.SYNC_ALL.equals(preProdStatus) || preProdStatus == null) {
            return null;
        }
        return preProdStatus.getCode();
    }
}
